iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 18
0
自我挑戰組

自我挑戰 Ruby 刷題 30 天系列 第 18

Day18 - Codewars 刷題

  • 分享至 

  • xImage
  •  

今天整個忙翻
擠出時間刷一題簡單的題目
示範如果對 Ruby 不熟時
可以如何下關鍵字、測試方法是否正確
Codewars LV8 小品一下


題目(Century From Year)

Introduction
The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc.

Task :
Given a year, return the century it is in.

Input , Output Examples ::
centuryFromYear(1705)  returns (18)
centuryFromYear(1900)  returns (19)
centuryFromYear(1601)  returns (17)
centuryFromYear(2000)  returns (20)
Hope you enjoy it .. Awaiting for Best Practice Codes

Enjoy Learning !!!
def century(year)
  # Your solution goes here, warrior
end

describe "Solution" do
  it "should return the proper century for each year" do
    Test.assert_equals(century(1705), 18, 'Testing for year 1705');
    Test.assert_equals(century(1900), 19, 'Testing for year 1900');
    Test.assert_equals(century(1601), 17, 'Testing for year 1601');
    Test.assert_equals(century(2000), 20, 'Testing for year 2000');
    Test.assert_equals(century(89), 1, 'Testing for year 89');
  end
end

影片解題:
Yes


答案:

# Century From Year
def century(year)
  year.ceil(-2)/100
end

本文同步發布於 小菜的 Blog https://riverye.com/


上一篇
Day17 - Codewars 刷題
下一篇
Day19 - Codewars 刷題
系列文
自我挑戰 Ruby 刷題 30 天31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言